import frida

import logging

logger = logging.getLogger('django')


class FridaControlCenter(object):
    def __init__(self):
        self.device_manager = None
        self.device = {}
        self.scripts = None
        self.package_name = None
        self.sessions = []
        self.frida_services = {}
        self.allAppList = {}
        self.usb_model = False

    def set_model(self, model):
        self.usb_model = model

    def set_package_name(self, pkg):
        self.package_name = pkg

    def get_package_name(self):
        return self.packagename

    def set_frida_services(self, fs):
        self.frida_services = fs

    def get_device(self):
        if self.usb_model:
            try:
                self.device['usb'] = frida.get_usb_device()
            except Exception as e:
                logger.error("frida API Error,No USB Device Found .Frida Error:{}".format(e))

        if self.device_manager is None:
            self.device_manager = frida.get_device_manager()

        for x in self.frida_services:
            try:
                dev = self.device_manager.add_remote_device(self.frida_services[x].ip + self.frida_services[x].port)
                self.device[x] = dev
            except Exception as e:

                continue

        # self.device = frida.get_usb_device()
        # self.device = frida.get_device_manager().enumerate_devices()[-1] # ios use in window  bug!!!
        return self.device
